package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.context.BaseContext;
import com.itheima.common.result.PageResult;
import com.itheima.common.result.Result;
import com.itheima.dto.YddArticlePageQueryDTO;
import com.itheima.mapper.YddArticleMapper;
import com.itheima.pojo.YddArticle;
import com.itheima.pojo.YddCate;
import com.itheima.service.YddArticleService;
import com.itheima.service.YddCateService;
import com.itheima.vo.YddArticleVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;


import java.util.ArrayList;
import java.util.List;

@Service
public class YddArticleServiceImpl extends ServiceImpl<YddArticleMapper, YddArticle> implements YddArticleService {

    @Autowired
    YddCateService yddCateService;

    /**
     * 文章列表-分页
     *
     * @param dto
     * @return
     */
    @Override
    public PageResult PageQuery(YddArticlePageQueryDTO dto) {
        IPage<YddArticle> page = new Page<>(dto.getPage(), dto.getLimit());
        LambdaQueryWrapper<YddArticle> lwq = new LambdaQueryWrapper<>();
        lwq.like(StringUtils.hasText(dto.getTitle()),YddArticle::getTitle, dto.getTitle());
        lwq.eq( StringUtils.hasText(dto.getCateid()),YddArticle::getCateid, dto.getCateid());
        if(StringUtils.hasText(dto.getStart_time())&& StringUtils.hasText(dto.getEnd_time())) {
            lwq.between(YddArticle::getCreateDate, dto.getStart_time(), dto.getEnd_time());
        }
        page(page, lwq);
        List<YddArticle> articleList = page.getRecords();
        List<YddArticleVo> list = new ArrayList();
        articleList.forEach(s -> {
            YddCate cate = yddCateService.getById(s.getCateid());

            YddArticleVo vo = new YddArticleVo();
            if(cate!=null) {
                vo = YddArticleVo.builder()
                        .catename(cate.getCatename())
                        .build();
            }
            BeanUtils.copyProperties(s, vo);
            list.add(vo);

        });

        return new PageResult(page.getTotal(), page.getCurrent(), list, page.getSize(), page.getPages());
    }

    /**
     * 文章详情
     *
     * @param id
     * @return
     */
    @Override
    public YddArticle rouInfoArticle(String id) {
        YddArticle article = getById(id);
        return article;
    }

    /**
     * 文章添加
     *
     * @param yddArticle
     * @return
     */
    @Override
    @Transactional
    public Result addArticle(YddArticle yddArticle) {
        yddArticle.setUpdateDate(System.currentTimeMillis());
        yddArticle.setDefaultData("1");
        Integer userId = Integer.valueOf(BaseContext.getCurrentId());
        yddArticle.setUserId(userId);
        save(yddArticle);
        return Result.success();
    }

    /**
     * 文章修改
     *
     * @param yddArticle
     * @return
     */
    @Override
    @Transactional
    public Result updateArticle(YddArticle yddArticle) {
        yddArticle.setUpdateDate(System.currentTimeMillis());
        updateById(yddArticle);
        return Result.success();
    }
}
